﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Mac
 *      Date:           2013/8/8 23:01:54
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Utility;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class RecipeDAO : DAOBase<M7_Recipe>,IRecipeDAO
    {
        #region Public functions
        public List<M7_Recipe> ReadAll()
        {
            return null;
        }

        /// <summary>
        /// 根据电子病历编号和单据状态获取所有处方单信息
        /// </summary>
        /// <returns></returns>
        public List<M7_Recipe> ReadAll(long emrId, int status)
        {
            string sql = @"SELECT 
                                master.*, 
                                detail.*, 
                                drug.*,  
                                specUnit.UnitId SpecUnitId,
                                specUnit.UnitName SpecUnitName,
                                specUnit.Flags SpecFlags,
                                usageUnit.UnitId UsageUnitId,
                                usageUnit.UnitName UsageUnitName,
                                usageUnit.Flags UsageFlags,
                                packUnit.UnitId PackUnitId,
                                packUnit.UnitName PackUnitName,
                                packUnit.Flags PackFlags
                           FROM 
                                M7_Recipe master, 
                                M7_RecipeItem detail, 
                                M7_Drug drug, 
                                M7_Unit specUnit, 
                                M7_Unit usageUnit,
                                M7_Unit packUnit
                         WHERE 1 = 1
                             and master.EmrId = @EmrId
                             and master.Status = @Status
                             and master.RecipeId = detail.RecipeId
                             and detail.DrugId = drug.DrugId
                             and drug.SpecUnitId = specUnit.UnitId
                             and drug.PackUnitId = packUnit.UnitId
                             and drug.SpecUnitId = usageUnit.UnitId";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(emrId, "EmrId"),
                    DBUtil.CreateSqlParameter(status, "Status"));

                List<M7_Recipe> recipeList = new List<M7_Recipe>();
                Dictionary<long, M7_Recipe> dic = new Dictionary<long, M7_Recipe>();

                while (reader.Read())
                {
                    M7_Recipe recipe = new M7_Recipe();
                    DBUtil.ReadData(reader, recipe);

                    List<M7_RecipeItem> itemList = null;
                    if (dic.ContainsKey(recipe.RecipeId))
                    {
                        itemList = dic[recipe.RecipeId].Items;
                    }
                    else
                    {
                        recipeList.Add(recipe);

                        dic.Add(recipe.RecipeId, recipe);
                        recipe.Items = new List<M7_RecipeItem>();
                        itemList = recipe.Items;
                    }

                    M7_RecipeItem item = new M7_RecipeItem();
                    DBUtil.ReadData(reader, item);

                    M7_Drug drug = new M7_Drug();
                    DBUtil.ReadData(reader, drug);
                    item.Drug = drug;

                    M7_Unit specUnit = new M7_Unit();
                    M7_Unit usageUnit = new M7_Unit();
                    M7_Unit packUnit = new M7_Unit();

                    object[] values = new object[reader.FieldCount];
                    reader.GetValues(values);

                    for (int i = 0; i < reader.FieldCount; ++i)
                    {
                        string propertyName = reader.GetName(i);
                        object propertyValue = values[i];

                        FillEntityUtil.ToUnit(specUnit, "Spec", propertyName, propertyValue);
                        FillEntityUtil.ToUnit(usageUnit, "Usage", propertyName, propertyValue);
                        FillEntityUtil.ToUnit(packUnit, "Pack", propertyName, propertyValue);
                    }

                    itemList.Add(item);
                }

                return recipeList;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="recipeItem"></param>
        /// <returns></returns>
        public override object Create(M7_Recipe recipe)
        {
            string sql = @"
                INSERT INTO M7_Recipe
                        (RecipeId
                        ,CitizenId
                        ,EmrId
                        ,CreateTime
                        ,UpdateTime
                        ,Amount
                        ,DeptId
                        ,DoctorId
                        ,PharmacistId
                        ,CashierId
                        ,Status)
                    VALUES
                        (@RecipeId
                        ,@CitizenId
                        ,@EmrId
                        ,@CreateTime
                        ,@UpdateTime
                        ,@Amount
                        ,@DeptId
                        ,@DoctorId
                        ,@PharmacistId
                        ,@CashierId
                        ,@Status)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(recipe.RecipeId, "RecipeId"),
                    DBUtil.CreateSqlParameter(recipe.CitizenId, "CitizenId"),
                    DBUtil.CreateSqlParameter(recipe.EmrId, "EmrId"),
                    DBUtil.CreateSqlParameter(recipe.CreateTime, "CreateTime"),
                    DBUtil.CreateSqlParameter(recipe.UpdateTime, "UpdateTime"),
                    DBUtil.CreateSqlParameter(recipe.Amount, "Amount"),
                    DBUtil.CreateSqlParameter(recipe.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(recipe.DoctorId, "DoctorId"),
                    DBUtil.CreateSqlParameter(recipe.PharmacistId, "PharmacistId"),
                    DBUtil.CreateSqlParameter(recipe.CashierId, "CashierId"),
                    DBUtil.CreateSqlParameter(recipe.Status, "Status"));
            }
        }
        #endregion
    }
}
